package InterviewTest.q0106_compressString;

public class Solution {
    public String compressString(String S) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < S.length(); i++) {
            char c = S.charAt(i);
            int count = 1;
            while (i + 1 < S.length() && S.charAt(i + 1) == c) {
                count++;
                i++;
            }
            sb.append(c);
            sb.append(count);
        }

        return sb.length() < S.length() ? sb.toString() : S;
    }
}
